﻿
using NicoView.Model.Platform.Browsers;
using NicoView.Model.Service;
using NicoView.Model.Share.Browser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NicoView.Model.Browser
{
    /// <summary>
    /// にこびゅで使用できるWebブラウザ
    /// </summary>
    public class Browsers : NicoViewObservableObject
    {
        private Browsers()
        {
            this.UrlExecuteBrowsers = new List<INicoViewBrowser>();

            this.setBrowsers(NicoViewBrowsersFactory.Current.CreateBrowsers());
        }

        /// <summary>
        /// URL解決ブラウザリスト
        /// </summary>
        public ICollection<INicoViewBrowser> UrlExecuteBrowsers;

        private INicoViewBrowser currenttUrlExecuteBrowser;

        /// <summary>
        /// 現在選択中のURL解決用ブラウザ
        /// </summary>
        public INicoViewBrowser CurrentUrlExecuteBrowser
        {
            get
            {
                return currenttUrlExecuteBrowser;
            }
            set
            {

                this.Set(() => this.CurrentUrlExecuteBrowser, ref this.currenttUrlExecuteBrowser, value);
            }
        }

        /// <summary>
        /// URL解決用ブラウザを名前指定で決定
        /// </summary>
        /// <param name="name"></param>
        internal void SetCurrenttUrlExecuteBrowser(UrlExecuteBrowser browser)
        {
            var selectBrowsers = from targetBrowser in this.UrlExecuteBrowsers
                                 where targetBrowser.UrlExecuteBrowser == browser
                                 select targetBrowser;

            if (selectBrowsers.Count() > 0)
            {
                this.CurrentUrlExecuteBrowser = selectBrowsers.First();
            }

            
        }


       

        private void setBrowsers(IEnumerable<INicoViewBrowser> browsers)
        {
            foreach(var browser in browsers)
            {
                this.UrlExecuteBrowsers.Add(browser);
            }
            CurrentUrlExecuteBrowser = UrlExecuteBrowsers.First();
        }

        /// <summary>
        /// 割り当てらているインスタンス
        /// </summary>
        public static readonly Browsers Instance = new Browsers();
    }
}
